package com.aaa.controller;

import com.aaa.config.DaysBetweenTool;
import com.aaa.config.GetEarningTool;
import com.aaa.pojo.*;
import com.aaa.service.*;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MyFinancialController {
    @Autowired
    private MyChange_MoneyService cmService;
    @Autowired
    private MyPay_MoneyService pmService;
    @Autowired
    private MyFund_ManagementService fmService;
    @Autowired
    private MyTerm_FinancialService tfService;
    @Autowired
    private MyLoanService loanService;
    @Autowired
    private BankNumberService bankService;
    //    核对个人交易码
    @Autowired
    private PersonUserService personUserService;
//    修改银行卡金额
    @Autowired
    private BankNumberService bankNumberService;

    /**
     * 返回给前台所有理财的信息
     * @return  所有理财信息
     */
    @RequestMapping(name = "所有理财信息",value = "/getManagementAll")
    public Map<String,Object> getManagementAll(HttpServletRequest request) throws ParseException {
        HttpSession session = request.getSession();
        Integer uid =(Integer) session.getAttribute("uid");
        Map<String, Object> dataMap = new HashMap<>();
        List<MyPay_Money> myPay_monies = pmService.selectByUid(uid);
        List<MyTerm_Financial> myTerm_financials = tfService.selectByUid(uid);
        List<MyChange_Money> myChange_monies = cmService.selectByUid(uid);
        List<MyFund_Management> myFund_managements = fmService.selectByUid(uid);
//计算收益
        GetEarningTool.getearning(myChange_monies,myPay_monies,myTerm_financials,myFund_managements);
        dataMap.put("myPays",myPay_monies);
        dataMap.put("myTerms",myTerm_financials);
        dataMap.put("myChanges",myChange_monies);
        dataMap.put("myFunds",myFund_managements);
        return dataMap;
    }
    //修改零钱理财状态   赎回
    @PostMapping(name = "修改零钱理财",value = "/updateMyChange")
    public Map<String,Object> updateMyChange(Integer mc_id,Integer bank_id,Double money,HttpServletRequest request) throws ParseException {
//        获取用户id
        HttpSession session = request.getSession();
        Integer uid =(Integer) session.getAttribute("uid");
        Map<String,Object> map=new HashMap<>();
//        修改银行卡金额
        Boolean McUpdate = bankNumberService.addMoneyById(bank_id, money);
        if(McUpdate==true){
            //        修改理财信息状态
            cmService.updateById(mc_id);
        }
        map.put("flag",true);
        List<MyChange_Money> myChange_monies = cmService.selectByUid(uid);
        GetEarningTool.getearning(myChange_monies,null,null,null);
        map.put("myChanges",myChange_monies);
        return map;
    }
    //修改工资理财状态   赎回
    @RequestMapping(name = "修改工资理财",value = "/updateMyPay")
    public Map<String,Object> updateMyPay(Integer mp_id,Integer bank_id,Double money,HttpServletRequest request) throws ParseException {
        HttpSession session = request.getSession();
        Integer uid =(Integer) session.getAttribute("uid");
        Map<String,Object> map=new HashMap<>();
//         修改银行卡金额
        Boolean mpUpdate = bankNumberService.addMoneyById(bank_id, money);
        if(mpUpdate==true){
            //     修改理财信息状态
            pmService.updateById(mp_id);
        }
        map.put("flag",true);
        List<MyPay_Money> myPay_monies = pmService.selectByUid(uid);
        GetEarningTool.getearning(null,myPay_monies,null,null);
        map.put("myPays",myPay_monies);
        return map;
    }
    //修改期限理财状态   赎回
    @RequestMapping(name = "修改期限理财",value = "/updateMyTerm")
    public Map<String,Object> updateMyTerm(Integer mt_id,Integer bank_id,Double money,HttpServletRequest request) throws ParseException {
        HttpSession session = request.getSession();
        Integer uid =(Integer) session.getAttribute("uid");
        Map<String,Object> map=new HashMap<>();
//        当银行卡金额成功修改后，通过mt_id修改期限理财状态
        if(bankNumberService.addMoneyById(bank_id,money)==true){
            tfService.updateById(mt_id);
        }
        map.put("flag",true);
        List<MyTerm_Financial> myTerm_financials = tfService.selectByUid(uid);
        GetEarningTool.getearning(null,null,myTerm_financials,null);
        map.put("myTerms",myTerm_financials);
        return map;
    }
    //修改基金理财状态   赎回
    @RequestMapping(name = "修改基金理财",value = "/updateMyFund")
    public Map<String,Object> updateMyFund(Integer mf_id,Integer bank_id,Double money,HttpServletRequest request) throws ParseException {
        HttpSession session = request.getSession();
        Integer uid =(Integer) session.getAttribute("uid");
        Map<String,Object> map=new HashMap<>();
        //        当银行卡金额成功修改后，通过mf_id修改基金理财状态
        if(bankNumberService.addMoneyById(bank_id,money)){
            fmService.updateById(mf_id);
        }
        map.put("flag",true);
        List<MyFund_Management> myFund_managements = fmService.selectByUid(uid);
        GetEarningTool.getearning(null,null,null,myFund_managements);
        map.put("myFunds",myFund_managements);
        return map;
    }
//    获取我的借贷信息
    @RequestMapping(name = "查询借贷信息",value = "/selectMyLoan/{currentPage}/{pageSize}")
    public PageInfo<MyLoan> selectMyLoan(@PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize, HttpServletRequest request) throws ParseException {
        HttpSession session = request.getSession();
        Integer uid =(Integer) session.getAttribute("uid");
        PageInfo<MyLoan> loanByPage = loanService.getLoanByPage(uid, currentPage, pageSize);
        GetEarningTool.getInterest(loanByPage.getList());
        return loanByPage;
    }
    @RequestMapping(name = "修改借贷信息",value = "/updateMyLoan")
    public Map<String, Object> updateMyLoan(String ml_id,int bank_id,Double money, HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer uid =(Integer) session.getAttribute("uid");
        Map<String,Object> map=new HashMap<>();
//        修改银行卡成功后，修改借贷状态
        if(bankNumberService.minusMoneyById(bank_id,money)==true){
            loanService.updateMyLoan(ml_id);
        }
        map.put("flag",true);
        return map;
    }
    @RequestMapping(name = "根据uid查询银行卡",value = "/selectBankByUid")
    public Map<String, Object> getBankByUid(HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer uid =(Integer) session.getAttribute("uid");
        Map<String, Object> map = new HashMap<>();
        List<BankNumber> bankNumbers = bankService.queryBankByUid(uid);
        map.put("banksinfo",bankNumbers);
        map.put("flag",true);
        return map;
    }
    @RequestMapping(name = "根据uid核对交易密码",value = "/checkDeal_Pwd")
    public Map<String, Object> getPersonDeal_Pwd(HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer uid =(Integer) session.getAttribute("uid");
        Integer deal_pwd = personUserService.selectPwdByUid(uid);
        Map<String, Object> map = new HashMap<>();
        map.put("flag",true);
        map.put("deal_pwd",deal_pwd);
        return map;
    }
}
